package com.ds.robot.assistant.util;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

/**
 * 作用：
 *
 * @author WeiShaoying
 * 
 */
@Component
public class DutyReader {

    private final ResourceLoader resourceLoader;

    public DutyReader(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

    // 通过ResourceLoader方式
    public String readDutyFile() {
        try {
            Resource resource = resourceLoader.getResource("classpath:duty.txt");
            BufferedReader reader = new BufferedReader(new InputStreamReader(resource.getInputStream()));
            StringBuilder content = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                content.append(line).append("\n");
            }
            return content.toString();
        } catch (IOException e) {
            throw new RuntimeException("读取duty.txt文件时发生错误", e);
        }
    }

    public String readDutyFile2() {
        ClassPathResource resource = new ClassPathResource("duty.txt");
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(resource.getInputStream()))) {
            StringBuilder content = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                content.append(line).append("\n");
            }
            return content.toString();
        } catch (IOException e) {
            throw new RuntimeException("读取duty.txt文件时发生错误", e);
        }
    }

    public static void main(String[] args) {
        // String dutyFile = new DutyReader().readDutyFile();
        // System.out.println(dutyFile);
        LocalDateTime dateTime = LocalDateTime.now().truncatedTo(ChronoUnit.HOURS);
        System.out.println("dateTime = " + dateTime);
        LocalDateTime dateTime2 = LocalDateTime.now().truncatedTo(ChronoUnit.MINUTES);
        System.out.println("dateTime = " + dateTime2);
    }

}
